//
//  UserViewController.m
//  享动
//
//  Created by darker on 16/4/9.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "UserViewController.h"
#import "UserTableViewCell.h"
#import "UserNameViewController.h"
#import "UserModel.h"

@interface UserViewController () <UITableViewDelegate, UITableViewDataSource, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) UserTableViewCell *cell;

// 头像
@property (nonatomic, strong) UIImageView *headImageView;

//  性别 身高 体重 生日 城市 toolBar
@property (weak, nonatomic) IBOutlet UIToolbar *genderToolBar;

@property (weak, nonatomic) IBOutlet UIToolbar *heightToolbar;

@property (weak, nonatomic) IBOutlet UIToolbar *weightToolBar;

@property (weak, nonatomic) IBOutlet UIToolbar *birthdayToolBar;

@property (weak, nonatomic) IBOutlet UIToolbar *cityToolBar;

// 年份 身高 体重 性别 城市 pickerView
@property (weak, nonatomic) IBOutlet UIPickerView *yearPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *heightPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *weightPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *genderPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *cityPickerView;


// 性别 年龄 身高 体重  省份 数组 / 城市字典
@property (nonatomic, strong) NSArray *genderArray;

@property (nonatomic, strong) NSMutableArray *ageArray;

@property (nonatomic, strong) NSMutableArray *heightArray;

@property (nonatomic, strong) NSMutableArray *weightArray;

@property (nonatomic, strong) NSArray *provinceArray;

@property (nonatomic, strong) NSDictionary *cityDic;

// 装载 所有PickerView 的视图
@property (weak, nonatomic) IBOutlet UIView *AllPickerView;

// 给头像视图 添加一个长按手势
@property (nonatomic, strong) UITapGestureRecognizer *tap;

@property (nonatomic, strong) NSString *userNameString;

// 修改数据时候 接收数据使用
@property (nonatomic, strong) NSString *name;

@property (nonatomic, strong) NSString *gender;

@property (nonatomic, strong) NSString *numberGender;

@property (nonatomic, strong) NSString *weight;

@property (nonatomic, strong) NSString *height;

@property (nonatomic, strong) NSString *age;

// 头像data
@property (nonatomic, strong) NSData *imgData;

@property (nonatomic, strong) NSMutableArray *modelArray;

@property (nonatomic, strong) NSDictionary *requestDic;

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;

@property (nonatomic, strong) NSMutableData *myRequestData;

@end

@implementation UserViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
    
    _tableView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 添加一个转圈圈控件
    _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    [_activityIndicatorView setCenter:CGPointMake(KWIDTH / 2, KHEIGHT / 2 - 30)];
    
    [self.tableView addSubview:_activityIndicatorView];
    
    // 请求数据
    [self request];

    // 性别
    _genderPickerView.frame = CGRectMake(0, 30, KWIDTH, 260);
    _genderPickerView.showsSelectionIndicator = YES;
    _genderPickerView.delegate = self;
    _genderPickerView.dataSource = self;
    _genderArray = [NSArray array];
    _genderArray = @[@"男", @"女"];
    
    
    // 生日
    _yearPickerView.frame = CGRectMake(0, 30, KWIDTH, 260);
    _yearPickerView.showsSelectionIndicator = YES;
    _yearPickerView.delegate = self;
    _yearPickerView.dataSource = self;
    
    _ageArray = [NSMutableArray array];
    for (int i = 5; i < 150; i++)
    {
        [_ageArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    // 身高
    _heightPickerView.frame = CGRectMake(0, 290, KWIDTH, 260);
    _heightPickerView.showsSelectionIndicator = YES;
    _heightPickerView.delegate = self;
    _heightPickerView.dataSource = self;
    
    _heightArray = [NSMutableArray array];
    for (int i = 30; i < 200; i++)
    {
        [_heightArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    // 体重
    _weightPickerView.frame = CGRectMake(0, 290, KWIDTH, 260);
    _weightPickerView.showsSelectionIndicator = YES;
    _weightPickerView.delegate = self;
    _weightPickerView.dataSource = self;
    
    _weightArray = [NSMutableArray array];
    for (int i = 5; i < 200; i++)
    {
        [_weightArray addObject:[NSString stringWithFormat:@"%d", i]];
    }

    // 省份
    _cityPickerView.frame = CGRectMake(0, 290, KWIDTH, 260);
    _cityPickerView.delegate = self;
    _cityPickerView.dataSource = self;
    _cityPickerView.showsSelectionIndicator = YES;

    //省
    _provinceArray = @[@"北京省", @"广西省", @"广东省", @"河北省",@"福建省"];
    //市
    _cityDic = @{
                 @"北京省":@[@"朝阳区", @"东城区", @"西城区"],
                 @"广西省":@[@"桂林市", @"南宁市"],
                 @"广东省":@[@"惠州市", @"广州市", @"深圳市",@"东莞市"],
                 @"河北省":@[@"石家庄",@"保定市", @"秦皇岛", @"唐山市", @"邯郸市",@"邢台市", @"沧州市", @"承德市", @"廊坊市", @"衡水市",@"张家口"],
                 @"福建省":@[@"福州市", @"宁德市", @"南平市", @"莆田市",@"泉州市",@"龙岩市",@"漳州市",@"三明市"]
                 };
    
    // 加载界面隐藏所有的toolBar
    _genderToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _weightToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _heightToolbar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _birthdayToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _cityToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;
}

#pragma mark --- 下拉刷新 ---
- (void) request
{
    [_activityIndicatorView startAnimating];
    
    NSDictionary *parDic = @{@"_ch":@"appStore", @"_lan":@"ch", @"_nw":@"WiFi", @"_product":@"fitmix", @"_sdk":@"iPhone%20OS8.2",@"_v":@"3", @"email":[[NSUserDefaults standardUserDefaults] objectForKey:@"name"], @"password":[[NSUserDefaults standardUserDefaults] objectForKey:@"password"], @"uid":@"0"};
    
    [RequestManager requestWithUrl:@"http://appt.igeekery.com/user/login.json" requestType:RequestTypePost parDic:parDic finish:^(NSData *data)
     {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *userDic = [dataDic objectForKey:@"user"];
        
        UserModel *model = [[UserModel alloc] init];
         
         _modelArray = [NSMutableArray array];
        
        [model setValuesForKeysWithDictionary:userDic];
        
        [_modelArray addObject:model];
        
         dispatch_async(dispatch_get_main_queue(), ^
         {
             [_tableView reloadData];
             
             [_tableView.mj_header endRefreshing];
             
             [_activityIndicatorView stopAnimating];
         });
        
    } error:^(NSError *error) {
        NSLog(@"用户界面 下拉刷新 数据请求错误");
        [_activityIndicatorView stopAnimating];
    }];
}

- (void) rightButtonAction
{
    UserModel *model = _modelArray[0];
    
    _numberGender = [NSString stringWithFormat:@"%ld", model.gender];
    _name = [NSString stringWithFormat:@"%@", model.name];
    _weight = [NSString stringWithFormat:@"%ld", model.weight];
    _height = [NSString stringWithFormat:@"%ld", model.height];
    _age = [NSString stringWithFormat:@"%ld", model.age];

    
    
    _requestDic = @{@"_ch":@"appStore", @"_lan":@"ch", @"_nw":@"WiFi", @"_product":@"fitmix", @"_sdk":@"iPhone OS8.2", @"_v":@"3", @"age":_age, @"gender":_numberGender,@"height":_height, @"k":@"1500c227736b4e3992d78931e7199a86", @"name":_name, @"type":@"1", @"uid":[[NSUserDefaults standardUserDefaults] objectForKey:@"id"], @"weight": _weight};
    
    [self request:_requestDic];
}

#pragma mark --- 城市取消button ---
- (IBAction)cityCancelButton:(id)sender
{
    [self cancel];
}

#pragma mark --- 城市完成button ---
- (IBAction)cityEndButton:(id)sender
{
    NSInteger provinceRow = [_cityPickerView selectedRowInComponent:0];
    NSString *provinceName = _provinceArray[provinceRow];
    NSArray *cityName = [_cityDic objectForKey:provinceName];
    NSInteger cityRow = [_cityPickerView selectedRowInComponent:1];
    
    [[NSUserDefaults standardUserDefaults] setObject:_provinceArray[provinceRow] forKey:@"province"];
    [[NSUserDefaults standardUserDefaults] setObject:cityName[cityRow] forKey:@"city"];
    
    [self ending];
    [self rightButtonAction];
}

#pragma mark --- 生日取消button ---
- (IBAction)birthDayCancelButton:(id)sender
{
    [self cancel];
}

#pragma mark --- 生日完成button ---
- (IBAction)birthDayEndButton:(id)sender
{
    NSInteger ageRow = [_yearPickerView selectedRowInComponent:0];
    
    UserModel *model = _modelArray[0];
    model.age = [_ageArray[ageRow] intValue];
    
    
    [self ending];
    [self rightButtonAction];
}

#pragma mark --- 体重完成button ---
- (IBAction)weightEndButton:(id)sender
{
    NSInteger weightRow = [_weightPickerView selectedRowInComponent:0];
    
    UserModel *model = _modelArray[0];
    model.weight = [_weightArray[weightRow] intValue];
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld", model.weight] forKey:@"userWeight"];
    
    [self ending];
    [self rightButtonAction];
}

#pragma mark --- 体重取消button ---
- (IBAction)weightCancelButton:(id)sender
{
    [self cancel];
}

#pragma mark --- 身高完成button ---
- (IBAction)heightEndButton:(id)sender
{
    NSInteger heightRow = [_heightPickerView selectedRowInComponent:0];
    
    UserModel *model = _modelArray[0];
    model.height = [_heightArray[heightRow] intValue];
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld", model.height] forKey:@"userHeight"];
    
    [self ending];
    [self rightButtonAction];
}

#pragma mark --- 身高取消button ---
- (IBAction)heightCancelButton:(id)sender
{
    [self cancel];
}

#pragma mark --- 性别完成button ---
- (IBAction)genderEndButton:(id)sender
{
    NSInteger row = [_genderPickerView selectedRowInComponent:0];
    
    UserModel *model = _modelArray[0];
    if ([_genderArray[row] isEqualToString:@"男"])
    {
        model.gender = 1;
    }
    else if ([_genderArray[row] isEqualToString:@"女"])
    {
        model.gender = 2;
    }

    [self ending];
    
    [self rightButtonAction];
}
#pragma mark --- 性别取消button ---
- (IBAction)cancelButton:(id)sender
{
    [self cancel];
}

#pragma mark --- 返回tableViewCell ---
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH - 70, 5, 40, 40)];
            _headImageView.backgroundColor = [UIColor grayColor];
            _headImageView.layer.cornerRadius = 20;
            _headImageView.userInteractionEnabled = YES;
            _headImageView.clipsToBounds = YES;
            _headImageView.image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"headImage"]];
            _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [_headImageView addGestureRecognizer:_tap];
            _cell.leftLabel.text = @"更改头像";
            
            [_cell.rightLabel removeFromSuperview];
            [_cell addSubview:_headImageView];
        }
    }
    
    
    if (indexPath.section == 1)
    {
        
        if (indexPath.row == 0)
        {
            UserModel *model = _modelArray[0];
            _cell.leftLabel.text = @"昵称";
            _cell.rightLabel.tag = 101;
            
            NSLog(@"昵称  ====  %@", [_cell.rightLabel.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
            
            _cell.rightLabel.text = model.name;
        }
        
        else if (indexPath.row == 1)
        {
            UserModel *model = _modelArray[0];
            
            if (model.gender == 1)
            {
                _cell.rightLabel.text = @"男";
            }
            else if (model.gender == 2)
            {
                _cell.rightLabel.text = @"女";
            }
            _cell.leftLabel.text = @"性别";
            
            
            
        }
        
        else if (indexPath.row == 2)
        {
            UserModel *model = _modelArray[0];
            _cell.leftLabel.text = @"身高";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%ldcm", model.height];
        }
        
        else if (indexPath.row == 3)
        {
            UserModel *model = _modelArray[0];
            
            _cell.leftLabel.text = @"体重";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%ldkg", model.weight];
        }
        
        else if (indexPath.row == 4)
        {
            UserModel *model = _modelArray[0];
            _cell.leftLabel.text = @"年龄";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%ld", model.age];
        }
        
//        else if (indexPath.row == 5)
//        {
//            _cell.leftLabel.text = @"所在地";
//            _cell.rightLabel.text = [NSString stringWithFormat:@"%@ %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"province"], [[NSUserDefaults standardUserDefaults] objectForKey:@"city"]];
//        }
    }
    
    if (indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            _cell.leftLabel.text = @"介绍";
        }
        else if (indexPath.row == 1)
        {
            _cell.introduceTextField.frame = CGRectMake(10, 0, KWIDTH, 50);
            _cell.introduceTextField.font = [UIFont systemFontOfSize:13];
            _cell.introduceTextField.textColor = [UIColor grayColor];
            _cell.introduceTextField.placeholder = @"个人介绍...";
            _cell.introduceTextField.delegate = self;
            [_cell addSubview:_cell.introduceTextField];
            // 给键盘添加两个方法
            [self registerForKeyboardNotifications];
        }
    }
    return _cell;
}

#pragma mark --- 点击cell 触发的方法 ---
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            // 弹出一个view  修改昵称
            UserNameViewController *userNameVc = [[UserNameViewController alloc] init];
            _cell = [tableView cellForRowAtIndexPath:indexPath];
            userNameVc.userName = _cell.rightLabel.text;
            [self.navigationController pushViewController:userNameVc animated:YES];
            
            __weak UserViewController *blockSelf = self;
            userNameVc.myUserName = ^(NSString *string){
                // 只有调用block的时候才运行这行代码
                blockSelf.cell.rightLabel.text = string;
                UserModel *model = _modelArray[0];
                model.name = string;
                [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"userName"];
                [self rightButtonAction];
            };
        }
        if (indexPath.row == 1)
        {
            // 弹出性别pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, 30, KWIDTH, 260) WeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 0, KWIDTH, 30) WeightTool:CGRectMake(0, 350, KWIDTH, 30) HeightTool:CGRectMake(0, 350, KWIDTH, 30) BirthdayTool:CGRectMake(0, 350, KWIDTH, 30) CityTool:CGRectMake(0, 350, KWIDTH, 30)];
            }];
        }
        if (indexPath.row == 2)
        {
            // 弹出身高pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                
                [_heightPickerView selectRow:140 inComponent:0 animated:NO];
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, 30, KWIDTH, 260) CityFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 290, KWIDTH, 30) WeightTool:CGRectMake(0, 290, KWIDTH, 30) HeightTool:CGRectMake(0, 0, KWIDTH, 30) BirthdayTool:CGRectMake(0, 290, KWIDTH, 30) CityTool:CGRectMake(0, 290, KWIDTH, 30)];
            }];
        }
        if (indexPath.row == 3)
        {
            // 弹出体重pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                [_weightPickerView selectRow:55 inComponent:0 animated:NO];
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame:CGRectMake(0, 30, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame: CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 290, KWIDTH, 30) WeightTool:CGRectMake(0, 0, KWIDTH, 30) HeightTool:CGRectMake(0, 290, KWIDTH, 30) BirthdayTool:CGRectMake(0, 290, KWIDTH, 30) CityTool:CGRectMake(0, 290, KWIDTH, 30)];
            }];
        }
        if (indexPath.row == 4)
        {
            // 弹出生日pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                // 设置初始显示值
                [_yearPickerView selectRow:20 inComponent:0 animated:NO];
                
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, 30, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame: CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 350, KWIDTH, 30) WeightTool:CGRectMake(0, 350, KWIDTH, 30) HeightTool:CGRectMake(0, 350, KWIDTH, 30) BirthdayTool:CGRectMake(0, 0, KWIDTH, 30) CityTool:CGRectMake(0, 350, KWIDTH, 30)];;
            }];
        }
//        if (indexPath.row == 5)
//        {
//            // 弹出城市pickerView 隐藏其他pickerView
//            [UIView animateWithDuration:0.5 animations:^{
//                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame:CGRectMake(0, 30, KWIDTH, 260) GenderTool:CGRectMake(0, 350, KWIDTH, 30) WeightTool:CGRectMake(0, 350, KWIDTH, 30) HeightTool:CGRectMake(0, 350, KWIDTH, 30) BirthdayTool:CGRectMake(0, 350, KWIDTH, 30) CityTool:CGRectMake(0, 0, KWIDTH, 30)];
//            }];
//        }
    }
}

#pragma mark --- 取消button 执行一样的方法 ---
- (void)cancel
{
    [UIView transitionWithView:_AllPickerView duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^
    {
        _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
    } completion:nil];
}

- (void)ending
{
    [UIView transitionWithView:_AllPickerView duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^
    {
        _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
    } completion:nil];
}

#pragma mark --- 返回tableView 每个分区的行数 ---
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else if (section == 1)
    {
        return 5;
    }
//    else if (section == 2)
//    {
//        return 2;
//    }
    return 0;
}

#pragma mark --- 返回tableView 分区个数 ---
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark --- 返回tableView 每行高度 ---
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark --- 设置每个pickerView frame 每个toolBar frame ---
- (void)setAllPickerViewFrame:(CGRect )allPickerViewFrame
                    YearFrame:(CGRect )yearFrame
                  GenderFrame:(CGRect )genderFrame
                  WeightFrame:(CGRect )weightFrame
                  HeightFrame:(CGRect )heightFrame
                    CityFrame:(CGRect )cityFrame
                   GenderTool:(CGRect )genderTool
                   WeightTool:(CGRect )weightTool
                   HeightTool:(CGRect )heightTool
                 BirthdayTool:(CGRect )birthdayTool
                     CityTool:(CGRect )cityTool
{
    _AllPickerView.frame = allPickerViewFrame;
    _yearPickerView.frame = yearFrame;
    _genderPickerView.frame = genderFrame;
    _weightPickerView.frame = weightFrame;
    _heightPickerView.frame = heightFrame;
    //_cityPickerView.frame = cityFrame;
    
    _genderToolBar.frame = genderTool;
    _weightToolBar.frame = weightTool;
    _heightToolbar.frame = heightTool;
    _birthdayToolBar.frame = birthdayTool;
    //_cityToolBar.frame = cityTool;
}


#pragma mark --- 返回每个pickerView 分区个数 ---
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (pickerView == _genderPickerView)
    {
        return 1;
    }
    else if (pickerView == _yearPickerView)
    {
        return 1;
    }
    else if (pickerView == _weightPickerView)
    {
        return 1;
    }
    else if (pickerView == _heightPickerView)
    {
        return 1;
    }
//    else if (pickerView == _cityPickerView)
//    {
//        return 2;
//    }
    return 0;
}

#pragma mark --- 返回每个人pickerView 分区的行数 ---
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == _genderPickerView)
    {
        if (component == 0)
        {
            return [_genderArray count];
        }
    }
    else if (pickerView == _yearPickerView)
    {
        if (component == 0)
        {
            return [_ageArray count];
        }
    }
    else if (pickerView == _heightPickerView)
    {

        return [_heightArray count];
        
    }
    else if (pickerView == _weightPickerView)
    {
        return [_weightArray count];
    }
//    else if (pickerView == _cityPickerView)
//    {
//        if (component == 0)
//        {
//            return [_provinceArray count];
//        }
//        if (component == 1)
//        {
//            NSInteger rowProvince = [_cityPickerView selectedRowInComponent:0];
//            NSString *provinceName = _provinceArray[rowProvince];
//            NSArray *citys = _cityDic[provinceName];
//            return citys.count;
//        }
//    }
    return 0;
}

#pragma mark --- 返回pickerView 每个分区每行的数据 ---
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == _genderPickerView)
    {
        return [_genderArray objectAtIndex:row];
    }
    else if (pickerView == _yearPickerView)
    {
        if (component == 0)
        {
            return [_ageArray objectAtIndex:row];
        }
    }
    else if (pickerView == _weightPickerView)
    {
        return [_weightArray objectAtIndex:row];
    }
    else if (pickerView == _heightPickerView)
    {
        return [_heightArray objectAtIndex:row];
    }
    else if (pickerView == _cityPickerView)
    {
        if (component == 0) {
            return _provinceArray[row];
        }
        if(component == 1)
        {
            NSInteger rowProvince = [_cityPickerView selectedRowInComponent:0];
            NSString *provinceName = _provinceArray[rowProvince];
            NSArray *citys = [_cityDic objectForKey:provinceName];
            return [citys objectAtIndex:row];
        }
    }
    return nil;
}

#pragma mark --- 点击pickerView 触发的方法 ---
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == _cityPickerView)
    {
        [_cityPickerView reloadComponent:1];
    }
}

#pragma mark --- 返回pickerView 的文本格式(包括高度 字体大小 颜色) ---
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (pickerView == _genderPickerView)
   {
       UILabel *pickerLabel = [[UILabel alloc] init];
       pickerLabel.textAlignment = NSTextAlignmentCenter;
       [pickerLabel setBackgroundColor:[UIColor clearColor]];
       [pickerLabel setFont:[UIFont boldSystemFontOfSize:14]];
       pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
       return pickerLabel;
    }
    UILabel *pickerLabel = [[UILabel alloc] init];
    pickerLabel.textAlignment = NSTextAlignmentCenter;
    [pickerLabel setBackgroundColor:[UIColor clearColor]];
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

#pragma mark --- 手势 触发的方法 ---
- (void) tapAction:(UITapGestureRecognizer *) tap
{
    dispatch_async(dispatch_get_main_queue(), ^
                   {
                       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleAlert];
                       
                       UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                       
                       UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"照相" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                           if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                           {
                               UIImagePickerController *picker =[[UIImagePickerController alloc]init];
                               picker.allowsEditing = YES;
                               picker.sourceType =  UIImagePickerControllerSourceTypeCamera;
                               
                               // 设置进入相册后 选取照片的代理 (执行 拍摄完成之后  或者选择完照片之后 自动调用的代理方法)
                               picker.delegate = self;
                               [self presentViewController:picker animated:YES completion:nil];
                           }
                           else
                           {
                               NSLog(@"没有相机");
                           }
                       }];
                       
                       UIAlertAction *selectPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                           
                           if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
                           {
                               UIImagePickerController *picker =[[UIImagePickerController alloc]init];
                               picker.allowsEditing = YES;
                               picker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
                               
                               // 设置进入相册后 选取照片的代理 (执行 拍摄完成之后  或者选择完照片之后 自动调用的代理方法)
                               picker.delegate = self;
                               [self presentViewController:picker animated:YES completion:nil];
                           }
                           else
                           {
                               NSLog(@"没有相册");
                           }
                       }];
                       [alert addAction:selectPhotoAction];
                       [alert addAction:photoAction];
                       [alert addAction:cancelAction];
                       [self presentViewController:alert animated:YES completion:nil];
                   });
}

#pragma mark --- ( 执行 拍摄完成之后  或者选择完照片之后 自动调用的代理方法 ) ---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 选取完毕之后  返回到原来的页面
    [self dismissViewControllerAnimated:YES completion:nil];
    // 将所选的照片赋值给 我们自己的imageView.image
    _headImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
    _imgData =  UIImagePNGRepresentation(img);
    [[NSUserDefaults standardUserDefaults] setObject:_imgData forKey:@"headImage"];
    
}

#pragma mark --- 给键盘添加两个方法 ---
- (void) registerForKeyboardNotifications
{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark --- 键盘出现时 ---
- (void) keyboardWasShown:(NSNotification *) notif
{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    _tableView.frame = CGRectMake(0, - keyboardSize.height, KWIDTH, KHEIGHT);
}

#pragma mark --- 键盘消失时 ---
- (void) keyboardWasHidden:(NSNotification *) notif
{
    _tableView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
}

#pragma mark --- 回收键盘 ---
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [_cell.introduceTextField resignFirstResponder];
    return YES;
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_cell.introduceTextField resignFirstResponder];
}

#pragma mark --- 返回表尾高度 ---
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0 || section == 1)
    {
        return 5.0;
    }
    return 0;
}

#pragma mark --- 修改数据时候的网络请求 ---
- (void)request:(NSDictionary *)dicData
{
    
    NSURL *url = [NSURL URLWithString:@"http://appt.igeekery.com/user/modify-info.json"];
    
    NSMutableString *bodyContent = [NSMutableString string];
    
    NSString *POST_BOUNDS = @"Boundary+EA9494BB9D231A97";
    
    
    
    for(NSString *key in dicData.allKeys)
    {
        id value = [dicData objectForKey:key];
        
        [bodyContent appendFormat:@"--%@\r\n", POST_BOUNDS];
        
        [bodyContent appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
        
        [bodyContent appendFormat:@"%@\r\n",value];
    }
    
    /*
     if (_imgData != nil)
     {
            UIImage *image = [UIImage imageWithData:_imgData];
         if (UIImagePNGRepresentation(image))
         {
         //返回为png图像。
          _imgData = UIImagePNGRepresentation(image);
         }
         else
         {
          //返回为JPEG图像。
          _imgData = UIImageJPEGRepresentation(image, 1.0);
         }
     
         UIImage *img = [UIImage imageWithData:_imgData];
         NSLog(@"%@", img);
         
         _myRequestData = [NSMutableData data];
         
         [bodyContent appendFormat:@"--%@\r\n", POST_BOUNDS];
         
         [bodyContent appendFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"318031_photo.jpg\"\r\n"];
         
         [bodyContent appendFormat:@"Content-Type: image/jpeg\r\n\r\n"];
         
         //将image的data加入
         [_myRequestData appendData:_imgData];
         
         //加入结束符--AaB03x--
         NSString *start = [[NSString alloc]initWithFormat:@"--%@\r\n", POST_BOUNDS];
         NSString *min = [[NSString alloc]initWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"318031_photo.jpg\"\r\n"];
         NSString *end = [[NSString alloc]initWithFormat:@"Content-Type: image/jpeg\r\n\r\n"];

         [_myRequestData appendData:[start dataUsingEncoding:NSUTF8StringEncoding]];
         [_myRequestData appendData:[min dataUsingEncoding:NSUTF8StringEncoding]];
         [_myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
     }
     */
    
    [bodyContent appendFormat:@"--%@--\r\n",POST_BOUNDS];
    
    NSData *bodyData=[bodyContent dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    
    
    
    [request addValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@", POST_BOUNDS] forHTTPHeaderField:@"Content-Type"];
    
    [request addValue: [NSString stringWithFormat:@"%zd",bodyData.length] forHTTPHeaderField:@"Content-Length"];
    //[request addValue: [NSString stringWithFormat:@"%zd",_myRequestData.length] forHTTPHeaderField:@"Content-Length"];

    [request setHTTPMethod:@"POST"];
    
    [request setHTTPBody:bodyData];
    
    NSLog(@"请求的长度%@",[NSString stringWithFormat:@"%zd",bodyData.length]);
    NSError *error=nil;
    NSURLResponse *response=nil;
    
    NSLog(@"输出Bdoy中的内容>>\n%@",[[NSString alloc]initWithData:bodyData encoding:NSUTF8StringEncoding]);
    
    NSData *reciveData= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    if(error)
    {
        NSLog(@"出现异常%@",error);
    }else
    {
        NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
        if(httpResponse.statusCode==200)
        {
            NSLog(@"服务器成功响应!>>%@",[[NSString alloc]initWithData:reciveData encoding:NSUTF8StringEncoding]);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
            });
            
        }else
        {
            NSLog(@"服务器返回失败>>%@",[[NSString alloc]initWithData:reciveData encoding:NSUTF8StringEncoding]);
            
        }
    }
}

#pragma mark --- 返回上一个界面button ---
- (void)leftButtonAction: (UIBarButtonItem *) button
{
    UserModel *model = _modelArray[0];

    if (_userHeadImage != nil)
    {
        _userHeadImage(_headImageView.image);
    }
    if (_userName != nil)
    {
        _userName(model.name);
    }
    
    // 创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = 1;
    
    //设置运动type
    animation.type = kCATransitionFromLeft;
    
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
